<template>
  <el-row loading>
    <el-col :span="24">
       <fleet-edit ref="fleetEdit"  v-on:save-finished="queryAll()"></fleet-edit>
      <el-row >
        <el-col :span="6">
          <el-input placeholder="请输入车队名称" size="small" v-model="search.name" style="width:100%">
          </el-input>
        </el-col>
        <el-col :span="6">
           <el-input placeholder="请输入车队编号" size="small" v-model="search.fleetNumber" style="width:100%">
          </el-input>
        </el-col>
        <el-col :span="6">
           <el-input placeholder="请输入负责人" size="small" v-model="search.manager" style="width:100%">
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="danger"  size="small" @click="queryAll()">搜索</el-button>
        </el-col>
        <el-col :span="24" style="text-align: left;">
          <el-button  icon="el-icon-plus" type="danger" @click="handleCreatefleet()" v-show="add">新增车队</el-button>
          <el-button  @click="handleCreatefleet()">车辆位置</el-button>
        </el-col>
      </el-row>
      </el-col>
       <el-col :span="24">
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="id" label="车队编号" header-align="center" width="120"></el-table-column>
            <el-table-column prop="name" label="车队名称" header-align="center" width="120"></el-table-column>
            <el-table-column prop="agencyId" label="所属机构" header-align="center" width="120"></el-table-column>
            <el-table-column prop="manager" label="负责人" header-align="center" width="120"></el-table-column>
            <el-table-column prop="truckCount" label="车辆总数" header-align="center" align="center" width="120">
              <template slot-scope="scope">
                  <span @click="truck(scope.row.id)" v-text="truckCount"></span>
              </template>
            </el-table-column>
            <el-table-column prop="" label="车辆详情" header-align="center" align="center" width="120">
              <template slot-scope="scope">
                <a href="truckDetail(scope.row.id)" >车辆列表</a>
              </template>
            </el-table-column>
            <el-table-column prop="driverCount" label="司机总数" header-align="center" align="center" width="120">
              <template slot-scope="scope">
                  <span v-text="driverCount" @click="driver(scope.row.id)"></span>
              </template>
            </el-table-column>
            <el-table-column prop="" label="司机详情" header-align="center" align="center" width="120">
              <template slot-scope="scope">
                <a href="driverDetail(scope.row.id)">司机列表</a>
              </template>
            </el-table-column>
            <el-table-column label="操作" header-align="center" align="center" width="200">
              <template slot-scope="scope">
                <el-button v-show="edit" type="primary" size="mini"
                  @click="handleEditfleet(scope.row)">编辑</el-button>
                <el-button  type="danger" size="mini"
                  @click="handleDeletetruckType(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row>
        <el-col :span="24">
         <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="currentChange"
            :current-page.sync="search.page"
            :page-sizes="[2,4,6,8]"
            :page-size="search.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="counts">
          </el-pagination>
        </el-col>
      </el-row>
        </el-col>
</el-row>
</template>
<script>
import teamApi from '@/api/team.js'
import FleetEdit from '@/components/fleet/fleetEdit'
  export default {
    data() {
      return {
        add:true,
        edit:true,
        tableData:[],
        truckCount:0,//车辆总数
        driverCount:0,//司机总数
        loading: true,
        search:{
        name:"",
        fleetNumber:"",
        manager:"",
        pageSize:2,//每页显示的条数
        page:1,//第一页
        },
        pages:"",//总共几页
        counts:0, //共几条

      }
    },
    methods: {
      queryAll(){
        teamApi.queryAll(this.search)
        .then(res => {
          this.tableData=res.data.items
          this.counts=res.data.counts;
        })
        .catch(err => {
          console.error(err);
        })
      },
      truck(id){
        teamApi.truckCounts(id).then(response => {
         this.truckCount=response.data
         console.log(this.truckCount)
          })
      },
      driver(id){
          teamApi.driverCounts(id).then(response => {
          this.driverCount=response.data
          console.log(this.driverCount)
        })
      },
      //车辆详情
      truckDetail(id){
        this.loading=true
        this.$refs.truckXinxi.$emit('openEditFleetDialog',response.data)
        this.loading = false
      },
      //司机详情
      driverDetail(id){

      },
      currentChange(val){
        this.page=val;
        this.queryAll();
      },
      handleSizeChange(val) {
        this.pageSize=val;
        this.queryAll();
        },
      handleCreatefleet(){
        this.loading = true
        this.$refs.fleetEdit.$emit('openAddFleetDialog')
        this.loading = false
      },
      handleEditfleet(row) {
      this.loading = true
      console.log(row)
      axios.put(""+row.id,row)
      .then(response => {
        this.$refs.fleetEdit.$emit('openEditFleetDialog',response.data)
        this.loading = false
      })
      .catch(error => {
        console.log(error)
      })
    },
    handleDeletetruckType(id){
       this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(() => {
        axios.put(""+id+"/disable")
        .then(res => {
         console.log(res.data)
         if(res.data.msg=="success"){
            this.$message({
            message: '删除了一条记录',
            type: 'warning'
        })
        this.queryAll()
         }
        })
    })
 }
},
    created(){
        this.queryAll();
        // this.truck();
        // this.driver();
    },
    components:{FleetEdit},

  }

</script>
